import axios from "axios"
import qs from "qs"
import router from "../../router/router"
import Cookies from "vue-cookies"


// console.log("当前环境",process.env.NODE_ENV);

/*第一层if判断生产环境和开发环境*/
if (process.env.NODE_ENV === 'production') {
    /*第二层if，根据.env文件中的VUE_APP_FLAG判断是生产环境还是测试环境*/
    if (process.env.VUE_APP_FLAG === 'pro') {
        //production 生产环境
        axios.defaults.baseURL = 'http://api.feifanblock.top';
        axios.defaults.baseImgURL = 'http://feifanblock.top';
        axios.defaults.baseRigisterURL = 'http://feifanblock.top';
    } else {
        //test 测试环境
        axios.defaults.baseURL = 'http://39.104.102.29:11886';
        axios.defaults.baseImgURL = 'http://39.104.102.29';
        axios.defaults.baseRigisterURL = 'http://39.104.102.29:11886/blockbank_web';
    }
} else {
    //dev 开发环境
    // axios.defaults.baseURL = 'http://192.168.1.33:11886';
    axios.defaults.baseURL = 'http://localhost:11886';
    axios.defaults.baseImgURL = 'http://192.168.1.16';
    axios.defaults.baseRigisterURL = 'http://localhost:8085';
    // axios.defaults.baseURL = 'http://api.feifanblock.top';
    // axios.defaults.baseImgURL = 'http://feifanblock.top';
    // axios.defaults.baseRigisterURL = 'http://feifanblock.top';
}

let baseUrl = axios.defaults.baseURL;

console.log("当前host：", baseUrl);

export default {
    requestWeb(type, url, data, callback) {
        axios({
            method: type ? type : 'post',
            url: url,
            data: qs.stringify({...data}),
            headers: {
                sid: Cookies.get("sid")
            }
        }).then((response) => {
            // console.log('------------>',response);
            if (response.data.code == 0) {
                callback(response.data)
            } else if (response.data.code == 2003 || response.data.code == 2004) {
                router.app.$toast(response.data.message);
                Cookies.remove('sid');
                Cookies.remove('user');
                router.app.$router.push('/m/login');
            } else {
                router.app.$toast({message:response.data.message,duration:1000});
            }
        }).catch((error)=>{
            console.log("error ==", error)
        });

    },

    requestWebFile(type, url, data, callback) {
        axios({
            method: type ? type : 'post',
            url: url,
            data: data,
            headers: {
                sid: Cookies.get("sid"),
                'Content-Type':'multipart/form-data',
            }

        }).then((response) => {
            // console.log('file ===========>',response);
            if (response.data.code == 0) {
                callback(response.data)
            } else if (response.data.code == 2003 || response.data.code == 2004) {
                router.app.$toast(response.data.message);
                Cookies.remove('sid');
                Cookies.remove('user');
                router.app.$router.push('/m/login');
            } else {
                router.app.$toast({message:response.data.message,duration:1000});
                router.app.$toast({message:'请重新上传',duration:1000,position: 'bottom'});
            }
        }).catch((error)=>{
            console.log("error ==", error)
        });

    },

    requestCMS(type, url, data, callback) {
        axios({
            method: type ? type : 'post',
            url: url,
            data: qs.stringify({...data}),
            headers: {
                sid: localStorage.getItem("sid")
            }
        }).then((response) => {
            // console.log('----->',response);
            if (response.data.code == 0) {
                callback(response.data)
            } else if (response.data.code == 2003 || response.data.code == 2004) {
                alert(response.data.message);
                localStorage.removeItem('sid');
                localStorage.removeItem('user');
                router.app.$router.push('/cms');
            } else {
                router.app.$notify({message:response.data.message,duration:2000});
            }
        }).catch((error)=>{
            console.log("error ==", error)
        });

    },

    requestCMSFile(type, url, data, callback) {
        axios({
            method: type ? type : 'post',
            url: url,
            data: data,
            headers: {
                sid: localStorage.getItem("sid")
            }
        }).then((response) => {
            // console.log('----->',response);
            if (response.data.code == 0) {
                callback(response.data)
            } else if (response.data.code == 2003 || response.data.code == 2004) {
                alert(response.data.message);
                localStorage.removeItem('sid');
                localStorage.removeItem('user');
                router.app.$router.push('/cms');
            } else {
                router.app.$notify({message:response.data.message,duration:2000});
            }
        }).catch((error)=>{
            console.log("error ==", error)
        });
    },
}
